$(function () {
    $("[name='wzbm-tree']").tree({
        url: $.webPath() + "/sys/wzbm/get_treejson?gsId=" + $("#gsIdwzbmedit").val(),
        onClick: function (node) {
            $("#wzbm-grid").datagrid(getGridOption(node.attributes.level, node.id));
        },
        onLoadSuccess: function () {
            $("[name='wzbm-tree']").tree('select', $("[name='wzbm-tree']").tree('getRoot').target);
        }
    });
    $("#wzbm-grid").datagrid({
        view: detailview,
        detailFormatter: function (index, row) {
            return '<div class="detail"></div>';
        },
        onExpandRow: function (index, row) {
            //先要关闭其他Detail
            var indexes = $('#wzbm-grid').datagrid('getRows').length;
            for (var i = 0; i < indexes; i++) {
                if (i != index)
                    $(this).datagrid("collapseRow", i);
            }
            //打开本行
            var ddv = $(this).datagrid('getRowDetail', index).find('div.detail');
            var url = $.webPath() + '/sys/wzbm/yjwzbm_content?index=' + index + '&level=0';
            if (row.id != null && row.id != undefined)
                url += '&wzbmId=' + row.id;
            ddv.panel({
                border: false,
                cache: true,
                href: url,
                onLoad: function () {
                    $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                    $('#wzbm-grid').datagrid('selectRow', index);
                }
            });
            $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
        },
        onCollapseRow: function (index, row) {
            if (!row.id)
                $(this).datagrid("deleteRow", index);
        },
        //双击第一个单元格展开
        onDblClickCell: function (rowIndex, field, value) {
            if (field == 'name') {
                $("#wzbm-grid").datagrid("expandRow", rowIndex);
            }
        },
        onLoadSuccess: function () {
            $("#wzbm_new").show();
            $("#wzbm_del").hide();
        },
        onSelect: function () {
            $("#wzbm_del").show();
        },
        url: $.webPath() + "/sys/wzbm/get_gridjson",
        fit: true,
        fitColumns: true,
        singleSelect: true,
        columns: [
            [
                {title: "名称", field: "name", width: 100},
                {title: "代码", field: "code", width: 100}
            ]
        ]
    });


})

function getGridOption(level, id) {
    var gsId = id.split(":")[2] == 0 ? "" : id.split(":")[2];
    id = id.split(":")[0];
    switch (level) {
        case "0":
            return {
                view: detailview,
                detailFormatter: function (index, row) {
                    return '<div class="detail"></div>';
                },
                onExpandRow: function (index, row) {
                    //先要关闭其他Detail
                    var indexes = $('#wzbm-grid').datagrid('getRows').length;
                    for (var i = 0; i < indexes; i++) {
                        if (i != index)
                            $(this).datagrid("collapseRow", i);
                    }
                    //打开本行
                    var ddv = $(this).datagrid('getRowDetail', index).find('div.detail');
                    var url = $.webPath() + '/sys/wzbm/yjwzbm_content?index=' + index + '&level=0&parentId=' + id;
                    if (row.id)
                        url += '&wzbmId=' + row.id;
                    ddv.panel({
                        border: false,
                        cache: true,
                        href: url,
                        onLoad: function () {
                            $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                            $('#wzbm-grid').datagrid('selectRow', index);
                        }
                    });
                    $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                },
                onCollapseRow: function (index, row) {
                    if (!row.id)
                        $(this).datagrid("deleteRow", index);
                },
                //双击第一个单元格展开
                onDblClickCell: function (rowIndex, field, value) {
                    if (field == 'name') {
                        $("#wzbm-grid").datagrid("expandRow", rowIndex);
                    }
                },
                onLoadSuccess: function () {
                    $("#wzbm_new").show();
                    $("#wzbm_del").hide();
                    $("#wzbm_import").hide();
                },
                onSelect: function () {
                    $("#wzbm_del").show();
                },
                url: $.webPath() + "/sys/wzbm/get_gridjson",
                pageNumber: 1,
                queryParams: {"level": level, "wzbmId": id, "gsId": gsId},
                columns: [
                    [
                        {title: "名称", field: "name", width: 100},
                        {title: "代码", field: "code", width: 100}
                    ]
                ]
            };
        case "1":
            return {
                view: detailview,
                detailFormatter: function (index, row) {
                    return '<div class="detail"></div>';
                },
                onExpandRow: function (index, row) {
                    //先要关闭其他Detail
                    var indexes = $('#wzbm-grid').datagrid('getRows').length;
                    for (var i = 0; i < indexes; i++) {
                        if (i != index)
                            $(this).datagrid("collapseRow", i);
                    }
                    //打开本行
                    var ddv = $(this).datagrid('getRowDetail', index).find('div.detail');
                    var url = $.webPath() + '/sys/wzbm/rjwzbm_content?index=' + index + '&level=1&parentId=' + id;
                    if (row.id != null && row.id != undefined)
                        url += '&wzbmId=' + row.id;
                    ddv.panel({
                        border: false,
                        cache: true,
                        href: url,
                        onLoad: function () {
                            $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                            $('#wzbm-grid').datagrid('selectRow', index);
                        }
                    });
                    $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                },
                onCollapseRow: function (index, row) {
                    if (!row.id)
                        $(this).datagrid("deleteRow", index);
                },
                //双击第一个单元格展开
                onDblClickCell: function (rowIndex, field, value) {
                    if (field == 'name') {
                        $("#wzbm-grid").datagrid("expandRow", rowIndex);
                    }
                },
                onLoadSuccess: function () {
                    $("#wzbm_new").show();
                    $("#wzbm_del").hide();
                    $("#wzbm_import").hide();
                },
                onSelect: function () {
                    $("#wzbm_del").show();
                },
                url: $.webPath() + "/sys/wzbm/get_gridjson",
                pageNumber: 1,
                queryParams: {"level": level, "wzbmId": id, "gsId": gsId},
                columns: [
                    [
                        {title: "名称", field: "name", width: 100},
                        {title: "代码", field: "code", width: 100},
                        {title: "所属一级物资编码", field: "yjwzbm_name", width: 100}
                    ]
                ]
            };
        case "2":
            return {
                view: detailview,
                detailFormatter: function (index, row) {
                    return '<div class="detail"></div>';
                },
                onExpandRow: function (index, row) {
                    //先要关闭其他Detail
                    var indexes = $('#wzbm-grid').datagrid('getRows').length;
                    for (var i = 0; i < indexes; i++) {
                        if (i != index)
                            $(this).datagrid("collapseRow", i);
                    }
                    //打开本行
                    var ddv = $(this).datagrid('getRowDetail', index).find('div.detail');
                    var url = $.webPath() + '/sys/wzbm/pm_content?index=' + index + '&level=2&parentId=' + id;
                    if (row.id != null && row.id != undefined)
                        url += '&wzbmId=' + row.id;
                    ddv.panel({
                        border: false,
                        cache: true,
                        href: url,
                        onLoad: function () {
                            $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                            $('#wzbm-grid').datagrid('selectRow', index);
                        }
                    });
                    $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                },
                onCollapseRow: function (index, row) {
                    if (!row.id)
                        $(this).datagrid("deleteRow", index);
                },
                //双击第一个单元格展开
                onDblClickCell: function (rowIndex, field, value) {
                    if (field == 'name') {
                        $("#wzbm-grid").datagrid("expandRow", rowIndex);
                    }
                },
                onLoadSuccess: function () {
                    $("#wzbm_new").show();
                    $("#wzbm_del").hide();
                    $("#wzbm_import").hide();
                },
                onSelect: function () {
                    $("#wzbm_del").show();
                },
                url: $.webPath() + "/sys/wzbm/get_gridjson",
                pageNumber: 1,
                queryParams: {"level": level, "wzbmId": id, "gsId": gsId},
                //title:"二级物资编码",
                columns: [
                    [
                        {title: "名称", field: "name", width: 100},
                        {title: "代码", field: "code", width: 100},
                        {title: "描述", field: "remark", width: 100},
                        {title: "所属二级物资编码", field: "rjwzbm_name", width: 100}
                    ]
                ]
            };
        case "3":
            return {
                view: detailview,
                detailFormatter: function (index, row) {
                    return '<div class="detail"></div>';
                },
                onExpandRow: function (index, row) {
                    //先要关闭其他Detail
                    var indexes = $('#wzbm-grid').datagrid('getRows').length;
                    for (var i = 0; i < indexes; i++) {
                        if (i != index)
                            $(this).datagrid("collapseRow", i);
                    }
                    //打开本行
                    var ddv = $(this).datagrid('getRowDetail', index).find('div.detail');
                    var url = $.webPath() + '/sys/wzbm/gg_content?index=' + index + '&level=3&parentId=' + id;
                    if (row.id != null && row.id != undefined)
                        url += '&wzbmId=' + row.id;
                    ddv.panel({
                        border: false,
                        cache: true,
                        href: url,
                        onLoad: function () {
                            $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                            $('#wzbm-grid').datagrid('selectRow', index);
                        }
                    });
                    $('#wzbm-grid').datagrid('fixDetailRowHeight', index);
                },
                onCollapseRow: function (index, row) {
                    if (!row.id)
                        $(this).datagrid("deleteRow", index);
                },
                //双击第一个单元格展开
                onDblClickCell: function (rowIndex, field, value) {
                    if (field == 'name') {
                        $("#wzbm-grid").datagrid("expandRow", rowIndex);
                    }
                },
                onLoadSuccess: function (index) {
                    $("#wzbm_new").show();
                    $("#wzbm_del").hide();
                    $("#wzbm_import").show();
                    $("#wzbm_import").data("id", id);
                },
                onSelect: function () {
                    $("#wzbm_del").show();
                },
                url: $.webPath() + "/sys/wzbm/get_gridjson",
                pageNumber: 1,
                queryParams: {"level": level, "wzbmId": id, "gsId": gsId},
                columns: [
                    [
                        {title: "名称", field: "name", width: 100},
                        {title: "代码", field: "code", width: 100},
                        {title: "规格型号", field: "ggxh", width: 100},
                        {title: "规格简称", field: "ggjc", width: 100},
                        {title: "材质", field: "cz", width: 100},
                        {title: "单位", field: "dw", width: 100},
                        {title: "是否报价", field: "sfbj", width: 100, formatter: function (value) {
                            if (value == '0')
                                return '否';
                            else
                                return '是';
                        }},
                        {title: "所属品名", field: "pm_name", width: 100}
                    ]
                ]
            };
    }
}


function wzbm_saveItem(index, level) {
    var url = "";
    switch (level) {
        case "0":
            if ($("[name='yjwzbm.id']").val() != null && $("[name='yjwzbm.id']").val() != "")
                url = $.webPath() + '/sys/wzbm/update_yjwzbm';
            else
                url = $.webPath() + '/sys/wzbm/add_yjwzbm';
            break;
        case "1":
            if ($("[name='rjwzbm.id']").val() != null && $("[name='rjwzbm.id']").val() != "")
                url = $.webPath() + '/sys/wzbm/update_rjwzbm';
            else
                url = $.webPath() + '/sys/wzbm/add_rjwzbm';
            break;
        case "2":
            if ($("[name='pm.id']").val() != null && $("[name='pm.id']").val() != "")
                url = $.webPath() + '/sys/wzbm/update_pm';
            else
                url = $.webPath() + '/sys/wzbm/add_pm';
            break;
        case "3":
            if ($("[name='gg.id']").val() != null && $("[name='gg.id']").val() != "")
                url = $.webPath() + '/sys/wzbm/update_gg';
            else
                url = $.webPath() + '/sys/wzbm/add_gg';
            break;
    }

    $('#wzbm-grid').datagrid('getRowDetail', index).find('form').form('submit', {
        url: url,
        onSubmit: function () {
            $.mask();
            if ($(this).form('validate')) {
                return true;
            } else {
                $.mask.hide();
                return false;
            }
        },
        success: function (data) {
            $.mask.hide();
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $('#wzbm-grid').datagrid('reload');
                $("[name='wzbm-tree']").tree('reload');
            } else
                $.messager.alert('提示', obj.message, 'info');
        }
    });
}

function wzbm_cancelItem(index) {
    var row = $('#wzbm-grid').datagrid('getRows')[index];
    if (row.isNewRecord) {
        $('#wzbm-grid').datagrid('deleteRow', index);
    } else {
        $('#wzbm-grid').datagrid('collapseRow', index);
    }
}


function wzbm_destroyItem() {
    var row = $('#wzbm-grid').datagrid('getSelected');
    if (row.isNewRecord) {
        $('#wzbm-grid').datagrid('deleteRow', row.indexOf($('#wzbm-grid').datagrid('getRows')));
    }
    var node = $("[name='wzbm-tree']").tree('getSelected');

    var level = node == null ? "0" : node.attributes.level;
    var url = "";
    switch (level) {
        case "0":
            url = $.webPath() + '/sys/wzbm/delete_yjwzbm';
            break;
        case "1":
            url = $.webPath() + '/sys/wzbm/delete_rjwzbm';
            break;
        case "2":
            url = $.webPath() + '/sys/wzbm/delete_pm';
            break;
        case "3":
            url = $.webPath() + '/sys/wzbm/delete_gg';
            break;
    }
    if (row) {
        $.messager.confirm('确认', '是否确认删除此物资编码', function (r) {
            if (r) {
                var index = $('#wzbm-grid').datagrid('getRowIndex', row);
                $.post(url, {wzbmId: row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200') {
                        $('#wzbm-grid').datagrid('deleteRow', index);
                        $("[name='wzbm-tree']").tree('reload');
                    } else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要删除的物资编码", 'info');
    }
}

function wzbm_newItem() {
    $("#wzbm_del").hide();
    $('#wzbm-grid').datagrid('insertRow', {index: 0, row: {isNewRecord: true}});
    //var index = $('#wzbm-grid').datagrid('getRows').length - 1;
    $('#wzbm-grid').datagrid('expandRow', 0);
    $('#wzbm-grid').datagrid('selectRow', 0);
}

function wzbm_search() {
    $('#wzbm-grid').datagrid($.extend(getGridOption("3", ""), {
        url: $.webPath() + "/sys/wzbm/search",
        queryParams: {
            'pm.name': $("#pm_name").val(),
            'ggxh': $("#gg_name").val(),
            'code': $("#gg_code").val(),
            'cz': $("#gg_cz").val()
        },
        onLoadSuccess: function () {
            $("#wzbm_new").hide();
            $("#wzbm_del").hide();
        },
        columns: [
            [
                {title: "名称", field: "name", width: 100},
                {title: "代码", field: "code", width: 100},
                {title: "规格型号", field: "ggxh", width: 100},
                {title: "规格简称", field: "ggjc", width: 100},
                {title: "材质", field: "cz", width: 100},
                {title: "单位", field: "dw", width: 100},
                {title: "是否报价", field: "sfbj", width: 100, formatter: function (value, row, index) {
                    return value ? '是' : '否'
                }},
                {title: "所属品名", field: "pm_name", width: 100}
            ]
        ]
    }));
}

$("#wzbm_import").click(function () {
    var id = $("#wzbm_import").data("id");
    var win = $.window({
        title: "物质编码信息导入",
        width: 450,
        height: 240,
        cache: false,
        url: $.webPath() + "/sys/wzbm/wzbm_upload?parentId=" + id,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $('#uploadSubject').form('submit', {
                        onSubmit: function () {
                            $(win).mask();
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#wzbm-grid').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
});



